//
// Created by ChenJi on 2022/1/5.
//
#include <adc.h>
#include "main.h"
#include "ZigbeeDevice.h"

uint16_t current_battery = 0;
uint8_t battery_inited = 0;
uint8_t isCharging = 0;

uint8_t Pocket_BatteryStateReply[4];
uint8_t Pocket_ChargeStateReply[2];

#define Error 1.0212

uint8_t Battery_Time_count = 0;

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)    //ADC转换完成回调
{
    HAL_ADC_Stop_IT(&hadc1);
    current_battery = HAL_ADC_GetValue(&hadc1);
    battery_inited = 1;

    Pocket_BatteryStateReply[1] = battery_inited;
}

void batteryManager() {
    Pocket_BatteryStateReply[0] = 0xAB;
    Pocket_ChargeStateReply[0] = 0xAC;
}

void updateChargeState(uint8_t charge_state) {
    isCharging = charge_state;

}
void sendChargeStateToTerminal(){
    Pocket_ChargeStateReply[1] = isCharging;
    replyToTerminal(Pocket_ChargeStateReply,2);
}
void sendBatteryToTerminal(){
    current_battery = Error*current_battery;
    Pocket_BatteryStateReply[2] = current_battery>>8;
    Pocket_BatteryStateReply[3] = current_battery;
    replyToTerminal(Pocket_BatteryStateReply,4);

}
void HC_1ms_Task_For_Battery_Check(){
    Battery_Time_count++;
    if(Battery_Time_count>=100) {
        HAL_ADC_Start_IT(&hadc1);
        updateChargeState(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7));
    }

}